#include "stdafx.h"
#include "DocumentManager.h"

#include <fstream>

DocumentManager::DocumentManager(std::string& filename)
{
	m_CollectionLength = 0;
	m_DocumentNumber = 0;
	
	m_Data = new DocumentData[250000];
	m_Data[0].index = 0;

	std::ifstream file(filename);
	while(file.good())
	{
		unsigned int index;
		file >> index;
		m_Data[index].index = index;
		file >> m_Data[index].documentName >> m_Data[index].length;
		m_CollectionLength += m_Data[index].length;
		m_DocumentIndexTable.insert(DocumentIndexTable::value_type(m_Data[index].documentName, index));
		m_DocumentNumber++;
	}

	file.close();
}

DocumentManager::~DocumentManager()
{
	delete[] m_Data;
}

DocumentData& DocumentManager::GetDocumentData(unsigned int index)
{
	return m_Data[index];
}

unsigned int DocumentManager::GetDocumentIndex(std::string& documentName)
{
	DocumentIndexTable::iterator it = m_DocumentIndexTable.find(documentName);
	if(it == m_DocumentIndexTable.end())
		return (unsigned int)-1;

	return it->second;
}